'''
This file is part of DopplerShift.

    DopplerShift is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DopplerShift is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DopplerShift.  If not, see <http://www.gnu.org/licenses/>.
'''
import os
from lxml import etree
from dopplershift.core.conf.pair import XMLPairConfig

class XMLCollectorConfig(XMLPairConfig):
    def _get_networking_settings(self, networking_service_type):
        for el in self._content.xpath('''//setting[@type='%s']'''%networking_service_type):
            return el.attrib#el.get('protocol'), el.get('host'), int(el.get('port')), bool(el.get('on'))
        return None, None, None, None
    def get_auth_service_settings(self):
        return self._get_networking_settings('auth')
    def get_cache_service_settings(self):
        return self._get_networking_settings('cache')
    def get_auths_backends(self):
        for el in self._content.xpath('''//auth/backend'''):
            yield el.attr
    def get_auth_backend(self, blackend_name):
        for el in self._content.xpath('''//auth/backend[@name='%s']'''%blackend_name):
            return el.attr
    def get_calls_tests(self):
        for el in self._content.xpath('''//calls/call/test'''):
            yield el
    def get_cache_key(self, cache_key):
        for el in self._content.xpath('''//caching/cache[@key='%s']'''%cache_key):
            return el.attr
    def get_routes(self):
        for el in self._content.xpath('''//routes'''):
            yield el
    def get_service(self, id):
        for el in self._content.xpath('''/*/calls/call[@id='%s']'''%(id)):
            return el
        
    def get_reload_template(self):
        for el in self._content.xpath('''//setting[@type='template_reload']/@on'''):
            return el
        return ''
    def get_upstream(self, upstream_id):
        for el in self._content.xpath('''//upstreams/upstream[@name='%s']'''%(upstream_id)):
            return el.get('resource')
        return False
    def get_templates_path(self):
        for el in self._content.xpath('''//settings/setting[@type='schema']'''):
            package, path = el.get('package'), el.get('path')
            if package != None:
                path = os.path.join(__import__(package).__path__[0], unicode(path))
            return unicode(path)
    def get_error_template_path(self, error_code):
        for el in self._content.xpath('''//settings/setting[@type='error_%s_page']'''%error_code):
            return el.get('path'), el.get('internal_redirect')
    def get_templates_all(self):
        for el in self._content.xpath('''//route/condition'''):
            yield unicode(el.get('template')), el.get('renderer')
    def get_test_define(self, test):
        for el in self._content.xpath('''//tests/%s'''%test):
            return el
    def get_setting(self, type):
        for el in self._content.xpath('''/*/settings/setting[@type='%s']'''%type):
            return dict(el.attrib.iteritems())